//$URL: http://dasding.googlecode.com/svn/trunk/data/dao/mongoDB/src/main/java/de/piratech/dasding/data/database/mongo/ActionCategoryDataImpl.java $
//$Id: ActionCategoryDataImpl.java 118 2013-04-01 01:11:50Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data.database.mongo;

import java.util.Arrays;
import java.util.List;

import com.mongodb.DB;

import de.piratech.dasding.data.Action;
import de.piratech.dasding.data.ActionCategory;
import de.piratech.dasding.data.User;
import de.piratech.dasding.data.database.ActionCategoryData;
import de.piratech.dasding.data.database.DatabaseException;
import de.piratech.dasding.data.database.mongo.data.MongoActionCategory;
import de.piratech.dasding.data.database.mongo.data.MongoUser;

/**
 */
public class ActionCategoryDataImpl extends
    AbstractMongoDB<MongoActionCategory, ActionCategory> implements
    ActionCategoryData {

  public ActionCategoryDataImpl(DB pDB) {
    super(pDB, MongoActionCategory.class, COLLECTIONS.ACTIONCATEGORY_COLLECTION);
  }

  @Override
  public ActionCategory createActionCategory(ActionCategory pActionCategory)
      throws DatabaseException {
    checkObjectExists(COLLECTIONS.USER_COLLECTION.getName(),
        pActionCategory.getCreatedBy());
    return getDBCollection().insert(new MongoActionCategory(pActionCategory))
        .getSavedObject().getDataObject();
  }

  @Override
  public ActionCategory getActionCategory(String pId) {
    return getDBCollection().findOneById(pId).getDataObject();
  }

  @Override
  public void updateActionCategory(ActionCategory pActionCategory) {
    getDBCollection().updateById(pActionCategory.getId(),
        new MongoActionCategory(pActionCategory));
  }

  @Override
  public void deleteActionCategory(ActionCategory pActionCategory)
      throws DatabaseException {
    List<Action> actions = new ActionDataImpl(db)
        .findActionsByCategories(Arrays.asList(new String[] { pActionCategory
            .getId() }));
    if (actions != null && actions.size() > 0) {
      throw new DatabaseException("there are related actions");
    }
    UserDataImpl userDataImpl = new UserDataImpl(db);
    List<User> users = userDataImpl.findUsersByActionCategory(pActionCategory);
    for (User user : users) {
      user.removeInterest(pActionCategory);
      userDataImpl.updateUser(user);
    }
    getDBCollection().removeById(pActionCategory.getId());
  }

  @Override
  public List<ActionCategory> getCategories() {
    List<MongoActionCategory> array = getDBCollection().find().toArray();
    List<ActionCategory> list = getDataObjectList(array);
    return list;
  }

  @Override
  public List<ActionCategory> getVerifiedCategories() {
    return findByVerified(true);
  }

  @Override
  public List<ActionCategory> getNotVerifiedCategories() {
    return findByVerified(false);
  }

  public List<ActionCategory> findByVerified(boolean pVerified) {
    List<MongoActionCategory> array = getDBCollection().find()
        .is("verified", pVerified).toArray();
    List<ActionCategory> list = getDataObjectList(array);
    return list;
  }

  public List<ActionCategory> findByCreated(User pUser) {
    List<MongoActionCategory> findByObjectReference = findByObjectReference(
        "createdByRef", COLLECTIONS.USER_COLLECTION.getName(),
        pUser.getId(), MongoUser.class);
    List<ActionCategory> list = getDataObjectList(findByObjectReference);
    return list;
  }
}
